﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.Setting
{
    internal sealed class SettingManager :  GameFrameworkModule, ISettingManager
    {
        ISettingMemory m_SettingMemory = null;
        public SettingManager()
        {
            m_SettingMemory = null;

            SetSettingMemory( new SettingMemory() );
            Load();
        }


        /// <summary>
        /// 获取游戏框架模块优先级。
        /// </summary>
        /// <remarks>优先级较高的模块会优先轮询，并且关闭操作会后进行。</remarks>
        internal override int Priority
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 游戏框架模块轮询。
        /// </summary>
        /// <param name="elapseSeconds">逻辑流逝时间，以秒为单位。</param>
        /// <param name="realElapseSeconds">真实流逝时间，以秒为单位。</param>
        internal override void Update(float elapseSeconds, float realElapseSeconds)
        { }

        /// <summary>
        /// 关闭并清理游戏框架模块。
        /// </summary>
        internal override void Shutdown()
        { }

        /// <summary>
        /// 设置 游戏数据存储器
        /// </summary>
        /// <param name="settingMemory"></param>
        public void SetSettingMemory(ISettingMemory settingMemory)
        {
            if (null == settingMemory)
            {

            }
            m_SettingMemory = settingMemory;
        }

        /// <summary>
        /// 时间片 轮询
        /// </summary>
        /// <param name="deltaTime"></param>
        public void OnUpdate( float deltaTime )
        {
            // 间隔时间去保存一次
            // Save();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public bool Load()
        {
            if (null == m_SettingMemory)
            {
                return false;
            }

            return m_SettingMemory.Load();
            
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public bool Save()
        {
            if (null == m_SettingMemory)
            {
                return false;
            }
            return m_SettingMemory.Save();
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public bool GetBool(string settingName)
        {
            if (null == m_SettingMemory)
            {
                return default( bool );
            }
            return m_SettingMemory.GetBool( settingName );
        }

        /// <summary>
        /// 获取数据
        /// 带缺省
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingDefault"></param>
        /// <returns></returns>
        public bool GetBool(string settingName, bool settingDefault)
        {
            if (null == m_SettingMemory)
            {
                return settingDefault;
            }
            return m_SettingMemory.GetBool( settingName, settingDefault );
        }

        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingValue"></param>
        public void SetBool(string settingName, bool settingValue)
        {
            if (null == m_SettingMemory)
            {
                return;
            }
            m_SettingMemory.SetBool( settingName, settingValue );
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public int GetInt(string settingName)
        {
            if (null == m_SettingMemory)
            {
                return default(int);
            }

            return m_SettingMemory.GetInt( settingName );
        }

        /// <summary>
        /// 获取数据
        /// 带缺省
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="setingDefault"></param>
        /// <returns></returns>
        public int GetInt(string settingName, int settingDefault)
        {
            if (null == m_SettingMemory)
            {
                return settingDefault;
            }

            return m_SettingMemory.GetInt(settingName, settingDefault);

        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingValue"></param>
        public void SetInt(string settingName, int settingValue)
        {
            if (null == m_SettingMemory)
            {
                return;
            }

            m_SettingMemory.SetInt( settingName, settingValue );
        }


        /// <summary>
        /// 获取数据 
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public float GetFloat(string settingName)
        {
            if (null == m_SettingMemory)
            {
                return default(float);
            }

            return m_SettingMemory.GetFloat(settingName);
        }

        /// <summary>
        /// 获取数据 
        /// 带缺省
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingDefault"></param>
        /// <returns></returns>
        public float GetFloat(string settingName, float settingDefault)
        {
            if (null == m_SettingMemory)
            {
                return settingDefault;
            }

            return m_SettingMemory.GetFloat(settingName, settingDefault);
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingValue"></param>
        public void SetFloat(string settingName, float settingValue)
        {
            if (null == m_SettingMemory)
            {
                return;
            }
            m_SettingMemory.SetFloat( settingName, settingValue);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public string GetString(string settingName)
        {
            if (null == m_SettingMemory)
            {
                return default(string);
            }

            return m_SettingMemory.GetString( settingName );
        }

        /// <summary>
        /// 获取数据 
        /// 带缺省
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingDefault"></param>
        /// <returns></returns>
        public string GetString(string settingName, string settingDefault)
        {
            if (null == m_SettingMemory)
            {
                return settingDefault;
            }

            return m_SettingMemory.GetString( settingName, settingDefault);
        }

        /// <summary>
        ///  设置数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingValue"></param>
        public void SetString(string settingName, string settingValue)
        {
            if (null == m_SettingMemory)
            {
                return;
            }
            m_SettingMemory.SetString(settingName, settingValue);
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public T GetObject<T>(string settingName)
        {
            if (null == m_SettingMemory)
            {
                return default( T );
            }
            return m_SettingMemory.GetObject<T>(settingName);
        }

        /// <summary>
        /// 获取数据
        /// 带缺省
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingDefault"></param>
        /// <returns></returns>
        public T GetObject<T>(string settingName, T settingDefault)
        {
            if (null == m_SettingMemory)
            {
                return settingDefault;
            }
            return m_SettingMemory.GetObject<T>(settingName, settingDefault);
        }

        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="settingValue"></param>
        public void SetObject<T>(string settingName, T settingValue)
        {
            if (null == m_SettingMemory)
            {
                return;
            }
            m_SettingMemory.SetObject<T>(settingName, settingValue);
        }

    }
}
